package com.ohc.leetcode.二叉树;

import com.ohc.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 2022年4月20日20:16:31
 */
public class 二叉树的层序遍历102 {
    static List<List<Integer>> lists = new ArrayList<>();

    public static List<Integer> levelOrder(TreeNode root) {
        if (root == null) {
            return null;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int count = queue.size();
            while (count > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);

                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                count--;

            }
            lists.add(list);
        }
        return null;
//        if (root == null) return null;
//        Queue<TreeNode> que = new LinkedList<TreeNode>();
//        que.offer(root);
//
//        while (!que.isEmpty()) {
//            List<Integer> itemList = new ArrayList<Integer>();
//            int len = que.size();
//
//            while (len > 0) {
//                TreeNode tmpNode = que.poll();
//                itemList.add(tmpNode.val);
//
//                if (tmpNode.left != null) que.offer(tmpNode.left);
//                if (tmpNode.right != null) que.offer(tmpNode.right);
//                len--;
//            }
//
//            lists.add(itemList);
//        }
//        return null;
    }

    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode();
        TreeNode treeNode2 = new TreeNode();
        TreeNode treeNode3 = new TreeNode();
        treeNode1.right = treeNode2;
        treeNode1.left = treeNode3;
        treeNode2.right = null;
        treeNode2.left = null;
        treeNode3.right = null;
        treeNode3.left = null;
        treeNode1.val = 1;
        treeNode2.val = 2;
        treeNode3.val = 3;
        List<Integer> list = levelOrder(treeNode1);
        lists.forEach(e -> {
            System.out.println("e = " + e);
        });
    }
}
